package com.mo.tools.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: e-Xingfu.Yang
 * @description: 全局线程池配置
 * @date:created in 2021/6/10 15:54
 * @modificed by:
 */

@Configuration
public class ThreadPoolExecutorConfig {


    /**
     * 8个核心线程
     * 30个最大线程
     * 线程活动60s
     * 任务拒绝后使用主线程继续执行
     * @return
     */
    /**
    *
    *
    * @author: e-Xingfu.Yang
    * @date: 2021/9/9 9:27
    * @return: java.util.concurrent.ThreadPoolExecutor
    * @throws: java.lang.Exception
    * @modificed by: 设置线程数为2倍cpu核心线程数，线程拒绝任务后继续用主线程执行
    */
    @Bean
    public ThreadPoolExecutor threadPool() {
        return new ThreadPoolExecutor(2 * Runtime.getRuntime().availableProcessors(),
                2 * Runtime.getRuntime().availableProcessors(),
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
